<?php 
//print_r($_POST['assign_wp']);
if ($_POST['assign_wp'] != "") $wp = implode(",",$_POST['assign_wp']); // else $wp = $_POST['assign_wp'];
if ($_POST['assign_rubrics'] != "") $rubrics = implode(",",$_POST['assign_rubrics']); else $rubrics = $_POST['assign_rubrics'];

if (($action == "edit") && ($go == "make-changes")) { 
	// will add another instance of the theme (a new version) to the DB

	if (($_POST['assign_overview'] == "") || ($_POST['assign_procedure'] == "")) { 
		// if the assignment overview and procedure ARE blank
		// insert new record into the db
		// get the new id of the record
		// redirect to alert user
		
		$insertSQL = sprintf("INSERT INTO assignments (cid, mid, vid, assign_num, assign_title, assign_overview, assign_procedure, assign_points, assign_rubrics, assign_wp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", 
			GetSQLValueString($cid, "int"),
			GetSQLValueString($mid, "int"),
			GetSQLValueString($_POST['vid'], "int"),
			GetSQLValueString($_POST['assign_num'], "text"),
			GetSQLValueString($_POST['assign_title'], "text"),
			GetSQLValueString($_POST['assign_overview'], "text"),
			GetSQLValueString($_POST['assign_procedure'], "text"),
			GetSQLValueString($_POST['assign_points'], "text"),
			GetSQLValueString($rubrics, "text"),
			GetSQLValueString($wp, "text")
		);
		
		//echo $insertSQL."<br>";
		mysql_select_db($database, $connection);
  		$result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
		$query_id_check = sprintf("SELECT id FROM assignments WHERE cid='%s' AND mid='%s' AND vid='%s'", $cid, $mid, $_POST['vid']);
		$id_check = mysql_query($query_id_check, $connection) or die(mysql_error());
		$row_id_check = mysql_fetch_assoc($id_check);
		
		header("location:../index.php?section=module_assignment&action=".$action."&go=missing&view=course_detail&dbTable=assignments&cid=".$cid."&mid=".$mid."&id=".$row_id_check['id']."&modal_window=".$modal_window."&msg=2");
	}
	
	else {
		// if the assignment overview and procedure are NOT blank
		// insert new record into the db
		// get the new id of the record
		// redirect to have user confirm the submission
		
		$insertSQL = sprintf("INSERT INTO assignments (cid, mid, vid, assign_num, assign_title, assign_overview, assign_procedure, assign_points, assign_rubrics, assign_wp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", 
			GetSQLValueString($cid, "int"),
			GetSQLValueString($mid, "int"),
			GetSQLValueString($_POST['vid'], "int"),
			GetSQLValueString($_POST['assign_num'], "text"),
			GetSQLValueString($_POST['assign_title'], "text"),
			GetSQLValueString($_POST['assign_overview'], "text"),
			GetSQLValueString($_POST['assign_procedure'], "text"),
			GetSQLValueString($_POST['assign_points'], "text"),
			GetSQLValueString($rubrics, "text"),
			GetSQLValueString($wp, "text")
		);	
		//echo $insertSQL."<br>";
		mysql_select_db($database, $connection);
  		$result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
		
		$query_id_check = sprintf("SELECT id FROM %s WHERE cid='%s' AND mid='%s' AND vid='%s'", $dbTable, $cid, $mid, $_POST['vid']);
		$id_check = mysql_query($query_id_check, $connection) or die(mysql_error());
		$row_id_check = mysql_fetch_assoc($id_check);
		
		$insertGoTo = "../index.php?section=module_assignment&action=".$action."&go=confirm&view=course_detail&dbTable=assignments&cid=".$cid."&mid=".$mid."&id=".$row_id_check['id']."&modal_window=".$modal_window."&msg=1";
		//echo $insertGoTo;
		header(sprintf("Location: %s", $insertGoTo));
	}
	
}

if ((($action == "add") || ($action == "edit") || ($action == "review")) && (($go == "draft") || ($go == "missing"))) {
	
	// generally, this is used by admins to edit an assignment without generating a new version
	
	if (($_POST['assign_overview'] == "") || ($_POST['assign_procedure'] == "")) {
		
		// if the assignment overview and procedure ARE blank
		// redirect to alert user
		
		$updateSQL = sprintf("UPDATE assignments
		SET 
		cid=%s,
		mid=%s,
		vid=%s,
		assign_num=%s, 
		assign_title=%s,
		assign_overview=%s,
		assign_procedure=%s,
		assign_points=%s,
		assign_rubrics=%s,
		assign_wp=%s
		WHERE id=%s", 
			GetSQLValueString($cid, "int"),
			GetSQLValueString($mid, "int"),
			GetSQLValueString($_POST['vid'], "int"),
			GetSQLValueString($_POST['assign_num'], "int"),
			GetSQLValueString($_POST['assign_title'], "text"),
			GetSQLValueString($_POST['assign_overview'], "text"),
			GetSQLValueString($_POST['assign_procedure'], "text"),
			GetSQLValueString($_POST['assign_points'], "text"),
			GetSQLValueString($rubrics, "text"),
			GetSQLValueString($wp, "text"),
			GetSQLValueString($id, "int")
		);
					
	mysql_select_db($database, $connection);
	$result1 = mysql_query($updateSQL, $connection) or die(mysql_error());
	//echo $updateSQL."<br>";
		
	header("location:../index.php?section=module_assignment&action=".$action."&go=missing&view=course_detail&dbTable=assignments&cid=".$cid."&mid=".$mid."&id=".$id."&modal_window=".$modal_window."&msg=2");
	}
	
	else {
	
	$updateSQL = sprintf("UPDATE assignments
		SET 
		cid=%s,
		mid=%s,
		vid=%s,
		assign_num=%s, 
		assign_title=%s,
		assign_overview=%s,
		assign_procedure=%s,
		assign_points=%s,
		assign_rubrics=%s,
		assign_wp=%s
		WHERE id=%s", 
			GetSQLValueString($cid, "int"),
			GetSQLValueString($mid, "int"),
			GetSQLValueString($_POST['vid'], "int"),
			GetSQLValueString($_POST['assign_num'], "text"),
			GetSQLValueString($_POST['assign_title'], "text"),
			GetSQLValueString($_POST['assign_overview'], "text"),
			GetSQLValueString($_POST['assign_procedure'], "text"),
			GetSQLValueString($_POST['assign_points'], "text"),
			GetSQLValueString($rubrics, "text"),
			GetSQLValueString($wp, "text"),
			GetSQLValueString($id, "int")
		);
					
	mysql_select_db($database, $connection);
	$result1 = mysql_query($updateSQL, $connection) or die(mysql_error());
	//echo $updateSQL."<br>";
	if (($go == "draft") || ($go == "missing")) $updateGoTo = "../index.php?section=module_assignment&action=".$action."&go=confirm&view=course_detail&dbTable=assignments&cid=".$cid."&mid=".$mid."&id=".$id."&modal_window=".$modal_window."&msg=1";
	else $updateGoTo = $updateGoTo."&msg=4";
	//echo $updateGoTo;
	header(sprintf("Location: %s", $updateGoTo));
	}
}


?>